package com.firebird.app.reader.pdf.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.firebird.app.reader.pdf.adapter.PdfFileAdapter
import com.firebird.app.reader.pdf.filehelper.FileConstant
import com.firebird.app.reader.pdf.filehelper.FileUtils
import com.firebird.app.reader.R
import java.io.File

class PdfFileScamperActivity : BaseActivity() {
    private var pdfListRv: RecyclerView? = null
    override  fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentLayout(R.layout.activity_pdf_file_scamper)
        init()
        initData()
    }

    private fun init() {
        pdfListRv = findViewById(R.id.apfs_rv)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        pdfListRv!!.addItemDecoration(DividerItemDecoration(
                this, DividerItemDecoration.VERTICAL))
        pdfListRv!!.layoutManager = layoutManager
    }

    private fun initData() {
        val sdPath: String = FileUtils.sDPath
                ?: //没有SD卡不能保存，可以添加提示
                return
        val folderFile = File(sdPath + FileConstant.PDF_PATH)
        if (!folderFile.exists() || !folderFile.isDirectory) {
            return
        }
        val allPdfFiles = folderFile.listFiles()
        if (allPdfFiles == null || allPdfFiles.size < 1) {
            return
        }
        val pdfFileAdapter = PdfFileAdapter(allPdfFiles)
        pdfListRv!!.adapter = pdfFileAdapter
        pdfFileAdapter.setOnItemClickLitener(object : PdfFileAdapter.OnItemClickLitener {
            override fun onItemClick(view: View?, position: Int) {
                val intent = Intent(this@PdfFileScamperActivity, ShowEditedPdfActivity::class.java)
                intent.putExtra("fileName", allPdfFiles[position].name)
                startActivity(intent)
            }
        })
    }
}